from flask import Flask, request, render_template, redirect, url_for
import pymysql
import json

conn = pymysql.connect('47.98.50.245', 'zgf', 'ZGFroot1234@', 'bili')
cur = conn.cursor()
app = Flask(__name__)


@app.route('/login', methods=['get', 'post'])
def login():
    data={
        'code': 0,
        'text': '未登录！'
    }
    if request.method == 'GET':
        return render_template('login.html')
    else:
        name = request.form['username']
        pwd = request.form['password']
        if name and pwd:
            sql = "select password from user where username='%s'" % (name,)
            cur.execute(sql)
            result = cur.fetchone()
            if result and pwd == result[0]:
                data['code'] = 1
                data['text'] = "登录成功"
                return render_template("index.html")
            else:
                return render_template('login.html')
        else:
            data['text'] = "登录失败！账号或密码错误！"
            return render_template('login.html')

@app.route('/reg', methods=['get', 'post'])
def reg():
    data={
        'code':-1,
        'text':"未注册！"
    }
    if request.method == 'GET':
        return render_template('reg.html')
    else:
        name = request.form['username']
        pwd = request.form['password']

        try:
            sql = "insert into user(username,password) values ('%s','%s')" % (name, pwd)
            cur.execute(sql)
            conn.commit()
            data['text'] = "注册完成！"
            data['code'] = 1
            print(data['text'])
            return render_template("index.html")
        except:
            data['text'] = "注册失败！"
            print(data['text'])
            return render_template('login.html')

# 第一页
@app.route('/index', methods=['get','post'])
def index():
    if request.method=='GET':
        return render_template('index.html')
    else:
        data = []
        sql = "select * from mini_video"
        try:
            cur.execute(sql)
            result = cur.fetchall()

            for row in result:
                id = row[0]
                tab_id = row[1]
                tag_id = row[2]
                title = row[3]
                author_id = row[4]
                author = row[5]
                description = row[6]
                likes = row[7]
                plays = row[8]
                created_at = row[9]
                share_url = row[10]
                first_pic = row[11]
                aa = {
                    'id': id,
                    'tab_id': tab_id,
                    'tag_id': tag_id,
                    'title': title,
                    'author_id': author_id,
                    'author': author,
                    'description': description,
                    'likes': likes,
                    'plays': plays,
                    'created_at': str(created_at),
                    'share_url': share_url,
                    'first_pic': first_pic
                }
                data.append(aa)
        except:
            pass

        return json.dumps(data)





# 第二页
@app.route('/view1', methods=['get','post'])
def view1():
    if request.method == 'GET':
       return render_template('view1.html')

    else:
        data=[]
        sql = "select * from mini_video where tab_id=49 "
        cur.execute(sql)
        result =cur.fetchall()
        try:
            for row in result:
                id = row[0]
                tab_id = row[1]
                tag_id = row[2]
                title = row[3]
                author_id = row[4]
                author = row[5]
                description = row[6]
                likes = row[7]
                plays = row[8]
                created_at = row[9]
                share_url = row[10]
                first_pic = row[11]
                aa = {
                    'id': id,
                    'tab_id': tab_id,
                    'tag_id': tag_id,
                    'title': title,
                    'author_id': author_id,
                    'author': author,
                    'description': description,
                    'likes': likes,
                    'plays': plays,
                    'created_at': str(created_at),
                    'share_url': share_url,
                    'first_pic': first_pic
                }
                data.append(aa)
        except:
            pass

    return json.dumps(data)

# 第三页
@app.route('/view2', methods=['get','post'])
def view2():
    if request.method == 'GET':
       return render_template('view2.html')

    else:
        data=[]
        sql = "select * from mini_video where tab_id=50"
        cur.execute(sql)
        result =cur.fetchall()
        try:
            for row in result:
                id = row[0]
                tab_id = row[1]
                tag_id = row[2]
                title = row[3]
                author_id = row[4]
                author = row[5]
                description = row[6]
                likes = row[7]
                plays = row[8]
                created_at = row[9]
                share_url = row[10]
                first_pic = row[11]
                aa = {
                    'id': id,
                    'tab_id': tab_id,
                    'tag_id': tag_id,
                    'title': title,
                    'author_id': author_id,
                    'author': author,
                    'description': description,
                    'likes': likes,
                    'plays': plays,
                    'created_at': str(created_at),
                    'share_url': share_url,
                    'first_pic': first_pic
                }
                data.append(aa)
        except:
            pass

    return json.dumps(data)


# 第四页
@app.route('/view3', methods=['get','post'])
def view3():
    if request.method == 'GET':
       return render_template('view3.html')

    else:
        data=[]
        sql = "select * from mini_video where tab_id=51"
        cur.execute(sql)
        result =cur.fetchall()
        try:
            for row in result:
                id = row[0]
                tab_id = row[1]
                tag_id = row[2]
                title = row[3]
                author_id = row[4]
                author = row[5]
                description = row[6]
                likes = row[7]
                plays = row[8]
                created_at = row[9]
                share_url = row[10]
                first_pic = row[11]
                aa = {
                    'id': id,
                    'tab_id': tab_id,
                    'tag_id': tag_id,
                    'title': title,
                    'author_id': author_id,
                    'author': author,
                    'description': description,
                    'likes': likes,
                    'plays': plays,
                    'created_at': str(created_at),
                    'share_url': share_url,
                    'first_pic': first_pic
                }
                data.append(aa)
        except:
            pass

    return json.dumps(data)

# 第五页
@app.route('/view4', methods=['get','post'])
def view4():
    if request.method == 'GET':
       return render_template('view4.html')

    else:
        data=[]
        sql = "select * from mini_video where tab_id=52"
        cur.execute(sql)
        result =cur.fetchall()
        try:
            for row in result:
                id = row[0]
                tab_id = row[1]
                tag_id = row[2]
                title = row[3]
                author_id = row[4]
                author = row[5]
                description = row[6]
                likes = row[7]
                plays = row[8]
                created_at = row[9]
                share_url = row[10]
                first_pic = row[11]
                aa = {
                    'id': id,
                    'tab_id': tab_id,
                    'tag_id': tag_id,
                    'title': title,
                    'author_id': author_id,
                    'author': author,
                    'description': description,
                    'likes': likes,
                    'plays': plays,
                    'created_at': str(created_at),
                    'share_url': share_url,
                    'first_pic': first_pic
                }
                data.append(aa)
        except:
            pass

    return json.dumps(data)


if __name__ == '__main__':
    app.run()
